<?php

class AlbumController extends ExtendedController
{
    private $model;
    function __construct()
    {
        $this->loadLibrary('Utility');
        $this->loadLibrary('Session');
        $this->model = $this->loadModel('album');
        parent::__construct();
    }

    public function index()
    {
        //var_dump($_SERVER);
        $albums = $this->model->getAlbumList();
        //var_dump($albums);
        $view = $this->loadView('album');
        $view->appendBlockData(array('albums'=>$albums));        
        $view->setBlockData('Quản lí các album ảnh','title');
        $view->render();
    }

    public function detail($id=null)
    {
        if (!isset($id) || $id=='')
        {
            Utility::redirect("album/index");
            return;
        }

        $album = $this->model->getAlbum($id);
        $items = $this->model->getAlbumItems($id);
        //var_dump($albums);
        $view = $this->loadView('detail');
        $view->appendBlockData(array('album'=>$album));
        $view->appendBlockData(array('items'=>$items));
        if (isset($message))
            $view->appendBlockData(array('message'=>$message));        
        $view->setBlockData('Chi tiết album','title');
        $view->render();
    }

    public function addalbum()
    {
        $name = (isset($_POST['name']))?$_POST['name']:null;
        if ($name != null)
        {
            $description = (isset($_POST['description']))?$_POST['description']:null;
            $status = $_POST['status'];

            $message = $this->model->addalbum($name, $description, $status);
            if ($message==null)
            {
                Utility::redirect("album/index");
                return;
            }
        }
        $view = $this->loadView('addalbum');     
        $view->setBlockData('Thêm album','title');        
        if (isset($message))
            $view->appendBlockData(array('message'=>$message));
        $view->render();
    }

    public function updateAlbum()
    {
        $id = (isset($_POST['id']))?$_POST['id']:null;
        $name = (isset($_POST['name']))?$_POST['name']:null;
        if ($name != null)
        {
            $description = (isset($_POST['description']))?$_POST['description']:null;
            $status = $_POST['status'];

            $message = $this->model->updateAlbum($id, $name, $description, $status);
        }

        Utility::redirect("album/detail/$id");
    }

    public function item($albumID=null, $id=null)
    {
        if ($id==null || $albumID==null)
        {
            Utility::redirect("album/index");
            return;
        }

        $description = (isset($_POST['description']))?$_POST['description']:null;
            if ($description != null)
            {
                $link = (isset($_POST['link']))?$_POST['link']:null;
                $this->model->updateItem($description, $link, $id);
                Utility::redirect("album/detail/$albumID");
            }

        $item = $this->model->getItem($id);

        $view = $this->loadView('item');
        $view->appendBlockData(array('albumID'=>$albumID));
        $view->appendBlockData(array('item'=>$item));
        $view->setBlockData('Cập nhật thông tin ảnh', 'title');
        if (isset($message))
             $view->appendBlockData(array('message'=>$message));
        $view->render();
    }

    public function additem($albumID=null)
    {
            $file = (isset($_FILES["file"]))?$_FILES["file"]:null;
            if ($file != null)
            {
                $description = (isset($_POST['description']))?$_POST['description']:null;
                $link = (isset($_POST['link']))?$_POST['link']:null;

                $message = $this->model->addItem($description, $file, $link, $albumID);
                if ($message==null)
                {
                    Utility::redirect("album/detail/$albumID");
                    return;
                }
            }
            $view = $this->loadView('additem');
            $view->appendBlockData(array('id'=>$albumID));
            $view->setBlockData('Thêm ảnh mới vào album', 'title');
            if (isset($message))
                $view->appendBlockData(array('message'=>$message));
            $view->render();
    }

    public function deleteAlbum($id=null)
    {
        if (!isset($id) || $id=='')
        {
            Utility::redirect("album/index");
            return;
        }
        
        $this->model->deleteAlbum($id);

        Utility::redirect("album/index");
    }

    public function deleteItem($id=null, $albumID=null)
    {
        if (!isset($id) || $id=='' || !isset($albumID) || $albumID=='')
        {
            Utility::redirect("album/index");
            return;
        }

        $this->model->deleteItem($id);

        Utility::redirect("album/detail/$albumID");
    }
}

